<?php
namespace app\api\controller;
use think\Db;

class Me extends Common {

    /**
     *我的活动
     */
    public function myactivites(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;
        $num = $page-1;
        $where['delete_type'] = 1;
        $res = Db::table('up_institutional_activities_member')
                ->alias('a')
                ->join('up_institutional_activities uia','uia.id = a.institutional_activities_id','left')
                ->join('up_mechanism um','um.mechanism_id = uia.activity_mechanism_id','left')
                ->where('a.user_id',$user_id)
                ->where($where)
                ->field(
                    'a.sign_status,
                    um.mechanism_name,
                    uia.activity_longitude,uia.activity_latitude,uia.id,
                    uia.activity_name,uia.activity_img,uia.activity_address,
                    uia.activity_start_time,uia.activity_end_time,uia.activity_status'
                )
                ->limit($num*10,10)
                ->select();
            $this->apiSuccess($res);

    }

    /***
     * 删除我的活动
     */
    public function  deletemyActivies(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $id = input('param.id');
        $activity_status =Db::table('up_institutional_activities')->where('id',$id)->value('activity_status');
        if($activity_status!=4){
            $this->apiError('E136');
        }
        $where['user_id'] = $user_id;//
        $where['institutional_activities_id'] = $id;
        $res = Db::table('up_institutional_activities_member')->where($where)->setField('delete_type',2);
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E001');
        }
    }

    /***
     * 我的课程收藏
     */
    public function myclasscollection(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;

        $num = $page-1;

        $res = Db::table('up_my_classroom')
                 ->alias('a')
                ->join('up_classroom uc','uc.id = a.classroom_id','left')
                ->field('uc.classroom_title,uc.classroom_img, uc.classroom_cate, uc.classroom_price,a.*')
                ->where('a.user_id',$user_id)
                ->limit($num*10,10)
                ->select();
            $this->apiSuccess($res);
    }
    /***
     * 我的观看历史
     */
    public function histroyclass(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;

        $num = $page-1;
        $res = Db::table('up_classroom_history')
            ->alias('a')
            ->join('up_classroom uc','uc.id = a.classroom_id','left')
            ->field('uc.classroom_title, uc.classroom_img, uc.classroom_cate, uc.classroom_price, a.*')
            ->where('a.user_id',$user_id)
            ->order('a.create_time desc')
            ->limit($num*10,10)
            ->select();
            $this->apiSuccess($res);
    }

    /**
     * 我的问答
     */
    public function myanswer(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type');
        $num = $page-1;
        if($type==1){
            $res = Db::table('up_interlocution')->where('user_id',$user_id)->limit($num*10,10)->select();
        }else{
            $res = Db::table('up_interlocution_answer')
                ->alias('a')
                ->join('up_interlocution ui','ui.interlocution_id = a.f_interlocution_id','left')
                ->where('a.user_id',$user_id)
                ->field('a.*,ui.interlocution_title')
                ->limit($num*10,10)
                ->select();
        }
            $this->apiSuccess($res);
    }

    /***
     * 删除我的课程收藏，观看历史
     */
    public function deletemyclass(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $type = input('param.type');
        $id = input('param.id');
        if($type==1){//删除收藏课程
           $res = Db::table('up_my_classroom')->where('id','IN',$id)->delete();

        }elseif ($type==2){//删除观看历史记录
            $res = Db::table('up_classroom_history')->where('history_id','IN',$id)->delete();
        }
        if($res){
            $this->apiSuccess();
        }else{
            $this->apiError('E001');
        }
    }

    /****
     * 公益历程，我的荣誉证书
     */
    public function myhonor(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        $page = input('param.page')?input('param.page'):1;
        $num = $page -1;
        $uid = ','.$user_id.',';
        $where['user_id'] = array('like',"%$uid%");
        $res = Db::table('up_my_welfare')->where($where)->field('welfare_id,welfare_title,create_time')->limit($num*10,10)->select();
        $this->apiSuccess($res);
    }

    /***
     * 荣誉证书详情
     */
    public function honordetail(){
        $welfare_id = input('param.welfare_id');
        $res = Db::table('up_my_welfare')->where('welfare_id',$welfare_id)->find();
        $this->apiSuccess($res);
    }
    /**
     * 我的证书
     */
    public function certificate(){
        extract(input());

        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            $this->apiError(301);
        }
        //用户信息
        $user_info =Db::table('up_u_user')
            ->alias('a')
            ->join('up_mechanism um','um.mechanism_id = a.user_to_mechanism_id','left')
            ->where('a.user_id',$user_id)
            ->field('a.user_sh_time,a.user_id,a.user_sh_time,a.user_username,a.user_gender,
     um.mechanism_name,a.user_join_time,a.user_number')
            ->find();
        $this->apiSuccess($user_info);

    }

}